package com.example.admin.rotate3danimation;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * @author Sodino E-mail:sodinoopen@hotmail.com
 * @version Time：2012-6-27 上午07:32:00
 */
public class ActRotate extends AppCompatActivity implements OnClickListener, RotateAnimation.InterpolatedTimeListener {
    private Button btnIncrease, btnDecrease, stop;
    private TextView txtNumber;
    private int number;
    /**
     * TextNumber是否允许显示最新的数字。
     */
    private boolean enableRefresh;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2);

        btnIncrease = (Button) findViewById(R.id.btnIncrease);
        btnDecrease = (Button) findViewById(R.id.btnDecrease);
        stop = (Button) findViewById(R.id.stop);
        txtNumber = (TextView) findViewById(R.id.txtNumber);

        btnIncrease.setOnClickListener(this);
        btnDecrease.setOnClickListener(this);
        stop.setOnClickListener(this);

        number = 30000;
        txtNumber = (TextView) findViewById(R.id.txtNumber);
        txtNumber.setText(Integer.toString(number));
    }

    public void onClick(View v) {
        if (v.getId() == R.id.stop) {
            txtNumber.getAnimation().cancel();
        } else {
            enableRefresh = true;
            RotateAnimation rotateAnim = null;
            float cX = txtNumber.getWidth() / 2.0f;
            float cY = txtNumber.getHeight() / 2.0f;
            if (v == btnDecrease) {
                number--;
                rotateAnim = new RotateAnimation(cX, cY, true);
            } else if (v == btnIncrease) {
                number++;
                rotateAnim = new RotateAnimation(cX, cY, false);
            }
            if (rotateAnim != null) {
                //rotateAnim.setInterpolatedTimeListener(this);
                rotateAnim.setFillAfter(true);
                rotateAnim.setLooping(true);
                txtNumber.startAnimation(rotateAnim);
            }
        }

    }

    @Override
    public void interpolatedTime(float interpolatedTime) {
        // 监听到翻转进度过半时，更新txtNumber显示内容。
        if (enableRefresh && interpolatedTime > 0.5f) {
            txtNumber.setText(Integer.toString(number));
            Log.d("ANDROID_LAB", "setNumber:" + number);
            enableRefresh = false;
        }
    }
}